package cn.net.autocode.platform.restApi;

import cn.hutool.core.util.RandomUtil;
import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.files.excel.ExcelTools;
import cn.net.autocode.platform.excel.service.ExcelServ;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;

@RestController
@RequestMapping("/platform/restApi")
public class PlatformRestApiCtrl {
    private final Dao dao;

    private final ExcelServ excelServ;

    public PlatformRestApiCtrl(Dao dao, ExcelServ excelServ) {
        this.dao = dao;
        this.excelServ = excelServ;
    }


    @RequestMapping("submitDraft")
    public ResultT submitDraft(@RequestBody JSONObject param){
        dao.executeUpdate("update sys_rest_api_record set status=? where id=?","approve",param.getString("id"));
        return ResultT.success();
    }

    @RequestMapping("approve")
    public ResultT approve(@RequestBody JSONObject param){
        String appKey = RandomUtil.randomString(32);
        String appSecret = RandomUtil.randomString(16);
        int num = dao.queryValue("select count(*) from sys_rest_api_record where app_key = ?",Integer.class,appKey);
        while(num>0){
            appKey = RandomUtil.randomString(16);
            num = dao.queryValue("select count(*) from sys_rest_api_record where app_key = ?",Integer.class,appKey);
        }

        dao.executeUpdate("update sys_rest_api_record set app_secret=?,app_key=?,approve_time=?,status=?,approval_comments=? where id=?",appSecret,appKey,new Date(),"Done",param.getString("reason"),param.getString("id"));

        return ResultT.success();
    }

    @RequestMapping("retire")
    public ResultT retire(@RequestBody JSONObject param){

        dao.executeUpdate("update sys_rest_api_record set app_secret=null,app_key=null,approval_comments=?,approve_time=?,status=? where id=?", param.getString("reason"),new Date(),"Draft",param.getString("id"));

        return ResultT.success();
    }

    @RequestMapping("/downLoadAuth")
    public ResponseEntity<byte[]> downLoadAuth(@RequestBody JSONObject param) {

        JSONObject template = excelServ.excelTemplate("apiAuth");
        JSONArray array = dao.queryJSONArray("select * from sys_rest_api_record where id=?",param.getString("id"));
        byte[] bytes = ExcelTools.writeExcel(template,array);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.add("file-name", URLEncoder.encode("授权信息.xlsx", StandardCharsets.UTF_8));
        headers.setContentDispositionFormData("attachment", URLEncoder.encode("授权信息.xlsx", StandardCharsets.UTF_8));
        return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
    }
}
